package com.liqi.display.service.impl;

import com.liqi.display.common.Constants;
import com.liqi.display.entity.returnType.TopTaskWindowNo;
import com.liqi.display.entity.UpProEval;
import com.liqi.display.mapper.UpProEvalMapper;
import com.liqi.display.service.IUpProEvalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liqi.display.utils.CalculateUtil;
import com.liqi.display.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liqi
 * @since 2021-04-13
 */
@Service
public class UpProEvalServiceImpl extends ServiceImpl<UpProEvalMapper, UpProEval> implements IUpProEvalService {
    private Logger logger = LoggerFactory.getLogger(UpProEvalServiceImpl.class);

    @Autowired
    UpProEvalMapper upProEvalMapper;

    @Override
    public Map<String, Map<String, Integer>> total() {
        Map<String,Map<String,Integer>>map=new ConcurrentHashMap<>();
        //当天满意度
        Date day = DateUtil.getDay();
        int totalFive = upProEvalMapper.total(5, day);
        int totalFour = upProEvalMapper.total(4, day);
        int totalThree = upProEvalMapper.total(3, day);
        int totalTwo = upProEvalMapper.total(2, day);
        int totalOne = upProEvalMapper.total(1, day);

        //当天好评数
        int goodDayTotal = totalFive + totalFour;
        //当天总评价数
        int dayTotal = totalFive + totalFour + totalThree + totalTwo + totalOne;

        Map<String,Integer>mapDay=new ConcurrentHashMap<>();
        mapDay.put("5",totalFive);
        mapDay.put("4",totalFour);
        mapDay.put("3",totalThree);
        mapDay.put("2",totalTwo);
        mapDay.put("1",totalOne);
        mapDay.put("goodDayTotal",goodDayTotal);
        mapDay.put("dayTotal",dayTotal);
        map.put("day",mapDay);

        //当月满意度
        Date month = DateUtil.getMonth();
        int totalFiveMonth = upProEvalMapper.total(5, month);
        int totalFourMonth = upProEvalMapper.total(4, month);
        int totalThreeMonth = upProEvalMapper.total(3, month);
        int totalTwoMonth = upProEvalMapper.total(2, month);
        int totalOneMonth = upProEvalMapper.total(1, month);

        //当月好评数
        int goodMonthTotal = totalFiveMonth +totalFourMonth;
        //当月总评价数
        int monthTotal = totalFiveMonth + totalFourMonth + totalThreeMonth + totalTwoMonth + totalOneMonth;

        Map<String,Integer>mapMonth=new ConcurrentHashMap<>();
        mapMonth.put("5",totalFiveMonth);
        mapMonth.put("4",totalFourMonth);
        mapMonth.put("3",totalThreeMonth);
        mapMonth.put("2",totalTwoMonth);
        mapMonth.put("1",totalOneMonth);
        mapMonth.put("goodMonthTotal",goodMonthTotal);
        mapMonth.put("monthTotal",monthTotal);
        map.put("month",mapMonth);

        //今年满意度
        Date year = DateUtil.getYear();
        int totalFiveYear = upProEvalMapper.total(5, year);
        int totalFourYear = upProEvalMapper.total(4, year);
        int totalThreeYear = upProEvalMapper.total(3, year);
        int totalTwoYear = upProEvalMapper.total(2, year);
        int totalOneYear = upProEvalMapper.total(1, year);

        //今年好评数
        int goodYearTotal = totalFiveYear + totalFourYear;
        //今年总评价数
        int yearTotal = totalFiveYear + totalFourYear + totalThreeYear + totalTwoYear + totalOneYear;

        Map<String,Integer>mapYear=new ConcurrentHashMap<>();
        mapYear.put("5",totalFiveYear);
        mapYear.put("4",totalFourYear);
        mapYear.put("3",totalThreeYear);
        mapYear.put("2",totalTwoYear);
        mapYear.put("1",totalOneYear);
        mapYear.put("goodYearTotal",goodYearTotal);
        mapYear.put("yearTotal",yearTotal);
        map.put("year",mapYear);



        return map;
    }

    @Override
    public List<TopTaskWindowNo> topTaskWindowNo() {
        List<TopTaskWindowNo> windowNos = upProEvalMapper.TopTaskWindowNo(DateUtil.getMonth());
        return windowNos;
    }

    /**
     * 当月专窗好评度概括
     * @return
     */
    @Override
    public Map<String,String> specilEval() {
        int rsTotal = 0; //人社总统计
        int rsGoodTotal = 0; //人社好评统计
        int gaTotal = 0; //公安
        int gaGoodTotal = 0; //公安好评统计
        int ybTotal = 0; //医保
        int ybGoodTotal = 0; //医保好评统计
        int swTotal = 0; //税务
        int swGoodTotal = 0; //税务好评统计
        int mzTotal = 0; //民政婚姻
        int mzGoodTotal = 0; //民政婚姻好评统计
        int gjjTotal = 0; //公积金
        int gjjGoodTotal = 0; //公积金好评统计
        int bdcTotal = 0; //不动产
        int bdcGoodTotal = 0; //不动产好评统计
        int qzhTotal = 0; //区综合
        int qzhGoodTotal = 0; //区综合好评统计
        int szhTotal = 0; //市综合
        int szhGoodTotal = 0; //市综合好评统计
        Map<String,String> map = new ConcurrentHashMap<>();
        Map<String,Integer> goodMap = new ConcurrentHashMap<>();
        Map<String,Integer> totalMap = new ConcurrentHashMap<>();
        try {
            List<TopTaskWindowNo> goodEvals = upProEvalMapper.specilEval(3, 5, DateUtil.getBeforeMonthFirst());//好评为3~5，统计出对应窗口号的好评数量
            List<TopTaskWindowNo> totalEvals =upProEvalMapper.specilEval(1,5,DateUtil.getBeforeMonthFirst());//统计全部
           if (goodEvals.size()!=0){
               for (TopTaskWindowNo g : goodEvals){
                   for (TopTaskWindowNo t :totalEvals){
                       if (g.getWindowno()!=null && g.getWindowno().equals(t.getWindowno())  ){
                           goodMap.put(g.getWindowno(),g.getNum());//传窗口编号，该窗口编号的好评数量和总数量
                           totalMap.put(t.getWindowno(),t.getNum());//传窗口编号，该窗口编号的总评价数量
                       }
                   }
               }
           }
           Map<String,Integer> goodEval = (Map<String,Integer>)goodMap;
           Map<String,Integer> totalEval = (Map<String,Integer>)totalMap;
           for (Map.Entry<String, Integer> total : totalEval.entrySet()) {
                for (Map.Entry<String, Integer> good : goodEval.entrySet()) {
                    if(total.getKey().equals(good.getKey())) {
                        Integer windowNo = Integer.valueOf(good.getKey());//窗口号
                        if(windowNo>=1&&windowNo<=15) {//人社0-15，包括人社综合业务、就业服务业务
                            rsTotal +=total.getValue();
                            rsGoodTotal +=good.getValue();
                        }
                        if(windowNo>=24&&windowNo<=61) {//公安24-61，户政业务、出入境业务、车驾管业务、治安业务、交警业务、公安出证
                            gaTotal +=total.getValue();
                            gaGoodTotal +=good.getValue();
                        }
                        if(windowNo>=16&&windowNo<=23) {//医保16-23，医保综合业务
                            ybTotal +=total.getValue();
                            ybGoodTotal +=good.getValue();
                        }
                        if(windowNo>=62&&windowNo<=81) {//税务62-81，办税综合业务、咨询导税业务
                            swTotal +=total.getValue();
                            swGoodTotal +=good.getValue();
                        }
                        if(windowNo>=100&&windowNo<=109) {//民政婚姻  离婚登记、民政综合、婚姻登记
                            mzTotal +=total.getValue();
                            mzGoodTotal +=good.getValue();
                        }
                        if(windowNo>=130&&windowNo<=139) {//公积金业务区 工商银行业务、农业银行业务、建设银行业务、中国银行业务、农商银行业务、邮政银行业务、公积金业务
                            gjjTotal +=total.getValue();
                            gjjGoodTotal +=good.getValue();
                        }
                        if(windowNo>=140&&windowNo<=158) {//不动产 不动产（税务）业务、不动产业务、不动产（财政）业务
                            bdcTotal +=total.getValue();
                            bdcGoodTotal +=good.getValue();
                        }
                        if(windowNo>=82&&windowNo<=129) {//城区综合业务区
                            qzhTotal +=total.getValue();
                            qzhGoodTotal +=good.getValue();
                        }
                        if(windowNo>=159&&windowNo<=164) {//市综合
                            szhTotal +=total.getValue();
                            szhGoodTotal +=good.getValue();
                        }
                    }
                }
           }
            map.put(Constants.RS_ZONE,CalculateUtil.proportionInt(rsGoodTotal,rsTotal));
            map.put(Constants.GA_ZONE,CalculateUtil.proportionInt(gaGoodTotal,gaTotal));
            map.put(Constants.YB_ZONE,CalculateUtil.proportionInt(ybGoodTotal,ybTotal));
            map.put(Constants.SW_ZONE,CalculateUtil.proportionInt(swGoodTotal,swTotal));
            map.put(Constants.MZ_ZONE,CalculateUtil.proportionInt(mzGoodTotal,mzTotal));
            map.put(Constants.GJJ_ZONE,CalculateUtil.proportionInt(gjjGoodTotal,gjjTotal));
            map.put(Constants.BDC_ZONE,CalculateUtil.proportionInt(bdcGoodTotal,bdcTotal));
            map.put(Constants.QZH_ZONE,CalculateUtil.proportionInt(qzhGoodTotal,qzhTotal));
            map.put(Constants.SZH_ZONE,CalculateUtil.proportionInt(szhGoodTotal,szhTotal));
           return map;
        }catch (Exception e){
            map.put(Constants.RS_ZONE,"99%");
            map.put(Constants.GA_ZONE,"98%");
            map.put(Constants.YB_ZONE,"90%");
            map.put(Constants.SW_ZONE,"97%");
            map.put(Constants.MZ_ZONE,"96%");
            map.put(Constants.GJJ_ZONE,"90%");
            map.put(Constants.BDC_ZONE,"99%");
            map.put(Constants.QZH_ZONE,"98%");
            map.put(Constants.SZH_ZONE,"96%");
            return map;
        }
    }

    /**
     * 前30天的好评率
     */
    @Override
    public Map<String, String> laterSeven() {
        Map<String,String> map = new ConcurrentHashMap<>();
        try {
            //Date late7Date = DateUtil.getLateDate("yyyy-MM-dd HH:mm:ss", 30);
            //改成近30天的统计数据
            //int notWorkDay = DateUtil.getNotWorkDay(30);
            //Date late7Date = DateUtil.getLateDate("yyyy-MM-dd HH:mm:ss", 30 + notWorkDay);
            
        	//计算工作日
            int notWorkDay = DateUtil.getNotWorkDay(1);
            //Date yesterday = DateUtil.getLateDate("yyyy-MM-dd HH:mm:ss", 1 + notWorkDay);
            Date yesterday = DateUtil.getLateDate("yyyy-MM-dd", 1 + notWorkDay);
        	
            //近1天的数据
            //Date yesterday = DateUtil.getYesterdayCurrent();
            
            int totalFive = upProEvalMapper.total(5, yesterday);
            int totalFour = upProEvalMapper.total(4, yesterday);
            int totalThree = upProEvalMapper.total(3, yesterday) + upProEvalMapper.total(2, yesterday) + upProEvalMapper.total(1, yesterday);
            /*20211006由于领导要过来检查，把不满意和非常不满意　合并到基本满意里面，已经在群里面讨论确认
             * int totalTwo = upProEvalMapper.total(2, late7Date);
            int totalOne = upProEvalMapper.total(1, late7Date);*/
            
            int totalTwo = 0;
            int totalOne = 0;

            //当天总评价数
            int total = totalFive + totalFour + totalThree + totalTwo + totalOne;

            map.put("fivePercent",totalFive + "");
            map.put("fourPercent",totalFour + "");
            map.put("threePercent",totalThree + "");
            map.put("twoPercent",totalTwo + "");
            map.put("onePercent",totalOne + "");
            map.put("total",total + "");
            
            logger.info("时间：{}",yesterday);
            logger.info("评价数据：{}",map);
            
            if(total != 0) {
            	double fiveRate = new BigDecimal(CalculateUtil.evalRate(total, totalFive)).multiply(new BigDecimal(100))
								.setScale(2,BigDecimal.ROUND_DOWN).doubleValue();
            	
            	double fourRate = new BigDecimal(CalculateUtil.evalRate(total, totalFour)).multiply(new BigDecimal(100))
								.setScale(2,BigDecimal.ROUND_DOWN).doubleValue();
            	double threeRate = new BigDecimal(100).subtract(new BigDecimal(fiveRate))
            			.subtract(new BigDecimal(fourRate)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            	map.put("fiveRate",fiveRate + "%");
            	map.put("fourRate",fourRate + "%");
            	map.put("threeRate",threeRate + "%");
            	map.put("twoRate","0.00%");
            	map.put("oneRate","0.00%");
            	
            	double temp = fiveRate + fourRate;
            	double goodEval = new BigDecimal(temp).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            	map.put("goodEvalRate",goodEval  + "%");
            }else {
            	map.put("fiveRate", "0.00%");
            	map.put("fourRate","0.00%");
            	map.put("threeRate","0.00%");
            	map.put("twoRate","0.00%");
            	map.put("oneRate","0.00%");
            	
            	map.put("goodEvalRate","0.00%");
            }
            
            return map;
        }catch (Exception e){
            logger.info("获取前七天好差评率异常,返回虚拟数据"+e.getMessage());
            map.put("fivePercent",96 + "");
            map.put("fourPercent",2 + "");
            map.put("threePercent",2 + "");
            map.put("twoPercent",0 + "");
            map.put("onePercent",0 + "");
            map.put("total",100 + "");
            
            map.put("fiveRate", "96.00%");
        	map.put("fourRate","2.00%");
        	map.put("threeRate","2.00%");
        	map.put("twoRate","0.00%");
        	map.put("oneRate","0.00%");
            map.put("goodEvalRate","98.00%");
            return map;
        }

    }

    /**
     * 近30天各业务区域好差评得分排名（满分5分）
     * @return
     */
	@Override
	public Map<String, String> scoreEval() {
		int rsNumTotal = 0; //人社评价个数相加
        int rsScoreTotal = 0; //人社分数相加
        int gaNumTotal = 0; //公安
        int gaScoreTotal = 0; //公安好评统计
        int ybNumTotal = 0; //医保
        int ybScoreTotal = 0; //医保好评统计
        int swNumTotal = 0; //税务
        int swScoreTotal = 0; //税务好评统计
        int mzNumTotal = 0; //民政婚姻
        int mzScoreTotal = 0; //民政婚姻好评统计
        int gjjNumTotal = 0; //公积金
        int gjjScoreTotal = 0; //公积金好评统计
        int bdcNumTotal = 0; //不动产
        int bdcScoreTotal = 0; //不动产好评统计
        int qzhNumTotal = 0; //区综合
        int qzhScoreTotal = 0; //区综合好评统计
        int szhNumTotal = 0; //市综合
        int szhScoreTotal = 0; //市综合好评统计
        Map<String,String> map = new ConcurrentHashMap<>();
        try {
        	//改成近30天的统计数据
            int notWorkDay = DateUtil.getNotWorkDay(30);
            Date work30Day = DateUtil.getLateDate("yyyy-MM-dd HH:mm:ss", 30 + notWorkDay);
           //List<TopTaskWindowNoNo> evalsList =upProEvalMapper.scoreEval(DateUtil.getBeforeMonthFirst());//统计全部
           List<TopTaskWindowNo> evalsList =upProEvalMapper.scoreEval(work30Day);//统计全部
           if(evalsList != null && evalsList.size() > 0 ){
        	   for(TopTaskWindowNo win : evalsList) {
/*        		   
        		   if(win.getNum() < 5) {
        			   System.out.println(win.getWindowno() + "该窗口当前评价小于5" + win.getNum());
        		   }*/
        		   
        		   Integer windowNo = Integer.valueOf(win.getWindowno());//窗口号
                   if(windowNo>=1&&windowNo<=15) {//人社0-15，包括人社综合业务、就业服务业务
                	   rsNumTotal++;
                       rsScoreTotal +=win.getNum();
                   }
                   if(windowNo>=24&&windowNo<=61) {//公安24-61，户政业务、出入境业务、车驾管业务、治安业务、交警业务、公安出证
                       gaNumTotal++;
                       gaScoreTotal +=win.getNum();
                   }
                   if(windowNo>=16&&windowNo<=23) {//医保16-23，医保综合业务
                       ybNumTotal++;
                       ybScoreTotal +=win.getNum();
                   }
                   if(windowNo>=62&&windowNo<=81) {//税务62-81，办税综合业务、咨询导税业务
                       swNumTotal++;
                       swScoreTotal +=win.getNum();
                   }
                   if(windowNo>=100&&windowNo<=109) {//民政婚姻  离婚登记、民政综合、婚姻登记
                       mzNumTotal++;
                       mzScoreTotal +=win.getNum();
                   }
                   if(windowNo>=130&&windowNo<=139) {//公积金业务区 工商银行业务、农业银行业务、建设银行业务、中国银行业务、农商银行业务、邮政银行业务、公积金业务
                       gjjNumTotal++;
                       gjjScoreTotal +=win.getNum();
                   }
                   if(windowNo>=140&&windowNo<=158) {//不动产 不动产（税务）业务、不动产业务、不动产（财政）业务
                       bdcNumTotal++;
                       bdcScoreTotal +=win.getNum();
                   }
                   if(windowNo>=82&&windowNo<=129) {//城区综合业务区
                       qzhNumTotal++;
                       qzhScoreTotal +=win.getNum();
                   }
                   if(windowNo>=159&&windowNo<=164) {//市综合
                       szhNumTotal++;
                       szhScoreTotal +=win.getNum();
                   }
        	   }
        	   
        	   map.put(Constants.RS_ZONE,CalculateUtil.scoreDivideNum(rsNumTotal,rsScoreTotal));
               map.put(Constants.GA_ZONE,CalculateUtil.scoreDivideNum(gaNumTotal,gaScoreTotal));
               map.put(Constants.YB_ZONE,CalculateUtil.scoreDivideNum(ybNumTotal,ybScoreTotal));
               map.put(Constants.SW_ZONE,CalculateUtil.scoreDivideNum(swNumTotal,swScoreTotal));
               map.put(Constants.MZ_ZONE,CalculateUtil.scoreDivideNum(mzNumTotal,mzScoreTotal));
               map.put(Constants.GJJ_ZONE,CalculateUtil.scoreDivideNum(gjjNumTotal,gjjScoreTotal));
               map.put(Constants.BDC_ZONE,CalculateUtil.scoreDivideNum(bdcNumTotal,bdcScoreTotal));
               map.put(Constants.QZH_ZONE,CalculateUtil.scoreDivideNum(qzhNumTotal,qzhScoreTotal));
               map.put(Constants.SZH_ZONE,CalculateUtil.scoreDivideNum(szhNumTotal,szhScoreTotal));
               
              return map;
           }else {
        	   map.put(Constants.RS_ZONE,"5");
               map.put(Constants.GA_ZONE,"4.79");
               map.put(Constants.YB_ZONE,"4.68");
               map.put(Constants.SW_ZONE,"4.63");
               map.put(Constants.MZ_ZONE,"4.59");
               map.put(Constants.GJJ_ZONE,"4.47");
               map.put(Constants.BDC_ZONE,"4.43");
               map.put(Constants.QZH_ZONE,"4.39");
               map.put(Constants.SZH_ZONE,"4.01");
               return map;
           }
        }catch (Exception e){
        	logger.error(e.toString(), e);
        	
            map.put(Constants.RS_ZONE,"5");
            map.put(Constants.GA_ZONE,"4.79");
            map.put(Constants.YB_ZONE,"4.68");
            map.put(Constants.SW_ZONE,"4.63");
            map.put(Constants.MZ_ZONE,"4.59");
            map.put(Constants.GJJ_ZONE,"4.47");
            map.put(Constants.BDC_ZONE,"4.43");
            map.put(Constants.QZH_ZONE,"4.39");
            map.put(Constants.SZH_ZONE,"4.01");
            return map;
        }
	}
    
    
}
